package com.enba.boot.mq.config;

import com.enba.boot.core.base.ShowBootInfo;
import com.enba.boot.mq.MessageSenderStrategy;
import com.enba.boot.mq.MqHelper;
import com.enba.boot.mq.factory.MqStrategyFactory;
import com.enba.boot.mq.properties.MqProperties;
import com.enba.boot.mq.sender.activemq.ActiveMqClusterMessageSenderStrategy;
import com.enba.boot.mq.sender.activemq.config.ActiveMQConfig;
import com.enba.boot.mq.sender.kafka.KafkaClusterMessageSenderStrategy;
import com.enba.boot.mq.sender.rabbitmq.RabbitmqMessageSenderStrategy;
import com.enba.boot.mq.sender.rocketmq.RocketmqClusterMessageSenderStrategy;
import java.util.List;
import javax.annotation.PostConstruct;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@EnableConfigurationProperties(MqProperties.class)
@Import(ActiveMQConfig.class)
public class MqAutoConfiguration implements ShowBootInfo {

  @Bean
  @ConditionalOnProperty(prefix = "mq.activemq", name = "enable", havingValue = "true")
  @ConditionalOnClass(name = "org.springframework.jms.core.JmsMessagingTemplate")
  public ActiveMqClusterMessageSenderStrategy activeMqClusterMessageSenderStrategy() {
    return new ActiveMqClusterMessageSenderStrategy();
  }

  @Bean
  @ConditionalOnProperty(prefix = "mq.kafka", name = "enable", havingValue = "true")
  @ConditionalOnClass(name = "org.springframework.kafka.core.KafkaTemplate")
  public KafkaClusterMessageSenderStrategy kafkaClusterMessageSenderStrategy() {
    return new KafkaClusterMessageSenderStrategy();
  }

  @Bean
  @ConditionalOnProperty(prefix = "mq.rabbitmq", name = "enable", havingValue = "true")
  @ConditionalOnClass(name = "org.springframework.amqp.rabbit.core.RabbitTemplate")
  public RabbitmqMessageSenderStrategy activeMqBroMessageSenderStrategy() {
    return new RabbitmqMessageSenderStrategy();
  }

  @Bean
  @ConditionalOnProperty(prefix = "mq.rocketmq", name = "enable", havingValue = "true")
  @ConditionalOnClass(name = "org.apache.rocketmq.spring.core.RocketMQTemplate")
  public RocketmqClusterMessageSenderStrategy rocketmqClusterMessageSenderStrategy() {
    return new RocketmqClusterMessageSenderStrategy();
  }

  @Bean
  public MqStrategyFactory mqStrategyFactory(
      List<MessageSenderStrategy> messageSenderStrategyList) {
    return new MqStrategyFactory(messageSenderStrategyList);
  }

  @Bean
  public MqHelper mqHelper(MqStrategyFactory mqStrategyFactory, MqProperties mqProperties) {
    return new MqHelper(mqStrategyFactory, mqProperties);
  }

  @PostConstruct
  @Override
  public void showBootInfo() {
    ShowBootInfo.moduleMap.put("enba-boot-starter-mq", "enba-boot-starter-mq模块集成成功");
  }
}
